from sympy import symbols, exp, diff
from math import sqrt

if __name__ == '__main__':
    x, y = symbols("x y")

    expression = x * exp(2 * y)

    # 计算通用的方向导数
    derivative_x = diff(expression, x)
    derivative_y = diff(expression, y)

    print("derivative_x = ", derivative_x)
    print("derivative_y = ", derivative_y)

    # 计算在 点 (1,0) 方向导数的值
    derivative_x_value = derivative_x.subs(x, 1).subs(y, 0)
    print("derivative_x_value = ", derivative_x_value)

    derivative_y_value = derivative_y.subs(x, 1).subs(y, 0)
    print("derivative_y_value = ", derivative_y_value)

    # 计算方向的值
    pq_length = sqrt((2 - 1) ** 2 + (-1 - 0) ** 2)
    # 第四象限
    # cos 值为正
    po_length = 2 - 1
    # sin 值为负
    oq_length = -1 - 0

    # 计算函数在点 (1,0) 沿着(1,-1) 方向导数的值
    direction_diff = derivative_x_value * (po_length / pq_length) + derivative_y_value * (oq_length / pq_length)
    print("direction_diff = ", direction_diff)
